package com.cennavi.minemap.tool;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import com.cennavi.minemap.dao.UploadDao2;

public class ParseZipFile implements ParseFile {

	public ParseZipFile(Map<String, String> parameter) {
		this.database = parameter.get("database");
		this.username = parameter.get("username");
		this.password = parameter.get("password");
		this.host = parameter.get("host");
		this.port = Integer.parseInt(parameter.get("port"));
		this.pyScript = parameter.get("pyScript");
		this.unzipPath = parameter.get("unzipPath");
	}

	static final int BUFFER = 2048;

	private String database = "minemap";
	private String username = "postgres";
	private String password = "superman";
	private String host = "123.207.109.239";
	private int port = 5432;
	private String pyScript = "UploadDataBatch.py";
	private String unzipPath = "E:\\test\\";

	@Override
	public Map<String, Integer> parse(String fileName, UploadDao2 uploadDao, int type, int userId, int uploadId) {
		// TODO Auto-generated method stub
		Map<String, Integer> rMap = new HashMap<String, Integer>();
		try {
			ZipFile zipFile = new ZipFile(fileName);

			Enumeration<? extends ZipEntry> emu = zipFile.entries();

			String path = "";

			while (emu.hasMoreElements()) {
				ZipEntry entry = (ZipEntry) emu.nextElement();
				// 会把目录作为一个file读出一次，所以只建立目录就可以，之下的文件还会被迭代到。
				if (entry.isDirectory()) {
					path = unzipPath + File.separator + entry.getName();
					new File(path).mkdirs();
					continue;
				}
				BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
				File file = new File(unzipPath + File.separator + entry.getName());
				// 加入这个的原因是zipfile读取文件是随机读取的，这就造成可能先读取一个文件
				// 而这个文件所在的目录还没有出现过，所以要建出目录来。
				File parent = file.getParentFile();
				if (parent != null && (!parent.exists())) {
					parent.mkdirs();
				}
				FileOutputStream fos = new FileOutputStream(file);
				BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);

				int count;
				byte data[] = new byte[BUFFER];
				while ((count = bis.read(data, 0, BUFFER)) != -1) {
					bos.write(data, 0, count);
				}
				bos.flush();
				bos.close();
				bis.close();
			}
			zipFile.close();

			String commond = "python " + pyScript + " " + path + " " + userId + " " + uploadId + " " + database + " "
					+ username + " " + password + " " + host + " " + port + " " + type;
			System.out.println(commond);
			Process pr = Runtime.getRuntime().exec(commond);

			BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
			String line;
			while ((line = in.readLine()) != null) {
				System.out.println(line);
				rMap.put("fileLine", Integer.parseInt(line.split(",")[1]));
				rMap.put("succLine", Integer.parseInt(line.split(",")[2]));
			}
			in.close();
			pr.waitFor();

			return rMap;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	// public static void main(String[] args) {
	// String fileName = "E://test//EXPORT_POI_DEMO.zip";
	//
	// Map<String, String> map = new HashMap<String, String>();
	//
	//
	// map.put("database", "minemap");
	// map.put("username", "postgres");
	// map.put("password", "superman");
	// map.put("host", "123.207.109.239");
	// map.put("port", "5432");
	//
	// map.put("pyScript", "UploadDataBatch.py");
	// map.put("unzipPath", "E:\\test\\");
	//
	// ParseFile parse = new ParseZipFile(map);
	// parse.parse(fileName, new UploadDao(), 1, 0, 0);
	//
	// }
}
